package cn.topcodes.tcsf.app.service.impl;

import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.admin.domain.entity.SysMenu;
import cn.topcodes.tcsf.admin.domain.form.NestableSort;
import cn.topcodes.tcsf.app.domain.entity.DailyActiveUsers;
import cn.topcodes.tcsf.app.domain.entity.HelpArticle;
import cn.topcodes.tcsf.app.service.HelpArticleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * HelpArticle 服务
 *
 * @author Yvan
 * @date 2018-01-26 15:50:03
 */
@Service
public class HelpArticleServiceImpl extends GenericService<HelpArticle> implements HelpArticleService {

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void saveSort(List<NestableSort> sort) {
        tranSaveSort(sort, null);
    }

    private void tranSaveSort(List<NestableSort> sort, Long parentId) {
        if (sort == null) return;
        for (int i = 0; i < sort.size(); i++) {
            NestableSort s = sort.get(i);
            if (null == s.getChildren() || s.getChildren().isEmpty()) {
                HelpArticle helpArticle = this.findOne(s.getId());
                helpArticle.setCatalogId(parentId);
                helpArticle.setSort(++i);
                this.update(helpArticle);
            }
            tranSaveSort(s.getChildren(), s.getId());
        }
    }
}
